require 'spec_helper'
require 'osx/cocoa'
require 'macos/game_controller'

describe STGameController do
  before(:each) do
    @controller = STGameController.alloc.init
    @game = mock("Game", :null_object => true)
    @controller.game = @game
  end
  
  it "should respond to #new" do
    @controller.should_not == nil
  end
  
  it "should have an empty array of views on creation" do
    @controller.should respond_to(:views)
    @controller.views.should == []
  end
  
  it "should update the game state when #tick is received" do
    @game.should_receive(:update).once
    @controller.tick
  end
  
  it "should send #update to any attached views when #tick is received" do
    view = mock("View")
    view.should_receive(:update).once
    
    @controller.views << view
    @controller.tick
  end
  
  it "should toggle the paused state when #toggle_paused is received" do
    pause = mock("PauseMenuItem", :null_object => true)
    
    @game.stub!(:paused?).and_return(true)
    @game.should_receive(:paused=).with(false)
    @controller.toggle_pause(pause)
    
    @game.stub!(:paused?).and_return(false)
    @game.should_receive(:paused=).with(true)
    @controller.toggle_pause(pause)
  end
  
  it "should change the pause menu item state" do
    pause = mock("PauseMenuItem")
    
    @game.stub!(:paused?).and_return(false)
    pause.should_receive(:state=).with(NSOffState)
    @controller.toggle_pause(pause)
    
    @game.stub!(:paused?).and_return(true)
    pause.should_receive(:state=).with(NSOnState)
    @controller.toggle_pause(pause)
  end
  
  it "should track key states" do
    event = mock("KeyDownEvent")
    event.stub!(:keyCode).and_return(KEY_UP_ARROW)
    
    @controller.game.stub!(:paused?).and_return(false)
    
    @controller.keyPressed?(KEY_UP_ARROW).should == false
    
    @controller.keyDown(event)
    @controller.keyPressed?(KEY_UP_ARROW).should == true
    
    @controller.keyUp(event)
    @controller.keyPressed?(KEY_UP_ARROW).should == false
  end
  
  it "should change the player's throttle when #throttle_changed is received" do
    slider = mock("NSSlider")
    slider.stub!(:floatValue).and_return(0.5)
    
    @controller.throttle_changed(slider)
    @controller.player.throttle.should == 0.5
  end
  
  it "should stop all player movement when the space bar is pressed" do
    @controller.game.stub!(:paused?).and_return(false)
    
    @controller.player.should_receive(:speed=).with(0)
    @controller.player.should_receive(:throttle=).with(0.0)
    @controller.player.should_receive(:rotation_vector=).with(0).twice
    
    e = mock("Event")
    e.stub!(:keyCode).and_return(KEY_SPACE)
    @controller.keyDown(e)
    @controller.keyPressed?(KEY_SPACE).should == true
    @controller.tick
  end
  
  it "should toggle displayCenterPoints? on #display_center_changed" do
    menu = mock("NSMenuItem")
    
    menu.should_receive(:state=).with(NSOnState)
    @controller.display_center_changed(menu)
    @controller.displayCenterPoints?.should == true
    
    menu.should_receive(:state=).with(NSOffState)
    @controller.display_center_changed(menu)
    @controller.displayCenterPoints?.should == false
  end
end